const { Sequelize, Model } = require("sequelize");
const { unset, clone, isArray } = require("lodash");
const {
  name,
  host,
  port,
  username,
  password,
} = require("../config/config").database;

const sequelize = new Sequelize(name, username, password, {
  dialect: "mysql",
  host,
  port,
  logging: true,

  timezone: "+08:00",
  define: {
    //create_time  update_time delete_time
    timestamps: true,
    paranoid: true,
    createdAt: "created_at",
    updatedAt: "updated_at",
    deletedAt: "deleted_at",
    underscored: true,
    freezeTableName: true,
    scopes: {
      bh: {
        attributes: {
          exclude: ["updated_at", "deleted_at", "created_at"],
        },
      },
    },
  },
});



sequelize.sync({
  force: false,
});


Model.prototype.toJSON = function () {

  let data = clone(this.dataValues);
  this.exclude
  unset(data,'created_at')
  if (isArray(this.exclude)) {
    this.exclude.forEach((el) => {
      unset(data, el);
    });
  }
  return data;
};
module.exports = { db: sequelize };
